﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using eSSP_example.Interface;
using eSSP_example.Entity;
using eSSP_example.Database;
using eSSP_example.Cpi;

namespace eSSP_example.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CashGenicService : ICashGenic
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPayRequestResponse doSession()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     getStatus
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiStatusResponse getStatus()
        {
            ApiStatusResponse   apiStatusResponse = new ApiStatusResponse();

            apiStatusResponse.cashStatus = ClsGlobalData.getInstance().buildCashStatus();
            apiStatusResponse.faceDeviceState = "faceDeviceState";
            apiStatusResponse.events = new List<ApiEvent>();
           
            return  apiStatusResponse;
        }

        /// <summary>
        ///     getSystemInfo
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiSystemInfoResponse getSystemInfo()
        {
            ApiSystemInfoResponse apiSystemInfoResponse = new ApiSystemInfoResponse();

            apiSystemInfoResponse.faceDeviceCapable = false;
            apiSystemInfoResponse.systemVersion = "1001";
            apiSystemInfoResponse.lowFloatWarnPercent = 0;
            apiSystemInfoResponse.maxPayoutLevel = 0;

            apiSystemInfoResponse.paymentDevices.Clear();
            List<ApiPaymentDevice> deviceList = ClsGlobalData.getInstance().buildPaymentDeviceList();

            foreach (ApiPaymentDevice device in deviceList)
            {
                apiSystemInfoResponse.paymentDevices.Add(device);
            }

            return  apiSystemInfoResponse;
        }

        private const int EXPIRE_MS = 12 * 60 * (60 * 1000);

        /// <summary>
        ///     getToken
        /// </summary>
        /// <param name="grant_type"></param>
        /// <param name="user_name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TokenResponse getToken(string grant_type, string username, string password)
        {
            //TokenResponse token = new TokenResponse();
            TokenResponse token;

            //Guid    uuid    =   Guid.NewGuid();
            //string  strUuid =   uuid.ToString();

            //token.access_token  =   strUuid;
            //token.expires_in    =   EXPIRE_MS;
            //token.userName      =   username; 
            //token.token_type    =   "bearer";

            LogUtils.Info("in getToken, grant_type=" + grant_type + ",username=" + username +
                    ",password=" + password);

            token = TokenStore.getInstance().putToken(username);
            
            return  token;
        }
    }
}
